﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class SpecialItem_Entity : EntityBase<SPECIALITEM>
    {
        public override RESP_Info Add(SPECIALITEM model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into SPECIALITEM(SPECIALITEM_ID,SPECIALITEM_NAME,SPECIALITEM_PRICE,SPECIALITEM_DES,SPECIALITEM_COVER) values (@SPECIALITEM_ID,@SPECIALITEM_NAME,@SPECIALITEM_PRICE,@SPECIALITEM_DES,@SPECIALITEM_COVER)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_ID", model.SPECIALITEM_ID));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_NAME", model.SPECIALITEM_NAME));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_PRICE", model.SPECIALITEM_PRICE));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_DES", model.SPECIALITEM_DES));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_COVER", model.SPECIALITEM_COVER));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加旅程特色项目成功</p></h5>";
                    info.Url = "SpecialItemList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加旅程特色项目失败</p></h5>";
                    info.Url = "SpecialItemAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加旅程特色项目失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(SPECIALITEM model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update SPECIALITEM set SPECIALITEM_NAME=@SPECIALITEM_NAME,SPECIALITEM_PRICE=@SPECIALITEM_PRICE,SPECIALITEM_DES=@SPECIALITEM_DES,SPECIALITEM_COVER=@SPECIALITEM_COVER where SPECIALITEM_ID=@SPECIALITEM_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_ID", model.SPECIALITEM_ID));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_NAME", model.SPECIALITEM_NAME));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_PRICE", model.SPECIALITEM_PRICE));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_DES", model.SPECIALITEM_DES));
                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_COVER", model.SPECIALITEM_COVER));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑旅程特色项目成功</p></h5>";
                    info.Url = "SpecialItemList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑旅程特色项目失败</p></h5>";
                    info.Url = "SpecialItemList.aspx";

                    Tom.Net.Log.LogManager.Error("编辑旅程特色项目失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(SPECIALITEM routeSpecialItem)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from SPECIALITEM where SPECIALITEM_ID=@SPECIALITEM_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_ID", routeSpecialItem.SPECIALITEM_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除旅程特色项目成功</p></h5>";
                    info.Url = "SpecialItemList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除旅程特色项目失败</p></h5>";
                    info.Url = "SpecialItemList.aspx";

                    Tom.Net.Log.LogManager.Error("删除旅程特色项目失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override SPECIALITEM GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select SPECIALITEM_ID,SPECIALITEM_NAME,SPECIALITEM_PRICE,SPECIALITEM_DES,SPECIALITEM_COVER from SPECIALITEM where SPECIALITEM_ID=@SPECIALITEM_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SPECIALITEM_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        SPECIALITEM routeSpecialItem = new SPECIALITEM();
                        if (!reader.IsDBNull(0)) routeSpecialItem.SPECIALITEM_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeSpecialItem.SPECIALITEM_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routeSpecialItem.SPECIALITEM_PRICE = reader.GetInt16(2);
                        if (!reader.IsDBNull(3)) routeSpecialItem.SPECIALITEM_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) routeSpecialItem.SPECIALITEM_COVER = reader.GetString(4);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeSpecialItem;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程特色项目数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override SPECIALITEM GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        SPECIALITEM routeSpecialItem = new SPECIALITEM();
                        if (!reader.IsDBNull(0)) routeSpecialItem.SPECIALITEM_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeSpecialItem.SPECIALITEM_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routeSpecialItem.SPECIALITEM_PRICE = reader.GetInt16(2);
                        if (!reader.IsDBNull(3)) routeSpecialItem.SPECIALITEM_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) routeSpecialItem.SPECIALITEM_COVER = reader.GetString(4);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeSpecialItem;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程特色项目数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<SPECIALITEM> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<SPECIALITEM> routeSpecialItems = new List<SPECIALITEM>();
                    while (reader.Read())
                    {
                        SPECIALITEM routeSpecialItem = new SPECIALITEM();
                        if (!reader.IsDBNull(0)) routeSpecialItem.SPECIALITEM_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeSpecialItem.SPECIALITEM_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routeSpecialItem.SPECIALITEM_PRICE = reader.GetInt16(2);
                        if (!reader.IsDBNull(3)) routeSpecialItem.SPECIALITEM_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) routeSpecialItem.SPECIALITEM_COVER = reader.GetString(4);

                        routeSpecialItems.Add(routeSpecialItem);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return routeSpecialItems;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程特色项目数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
